package com.itheima.wisdomnext.config;

import com.itheima.wisdomnext.Handler.WebSocketHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

@Configuration
@EnableWebSocket  // 启用 WebSocket 配置
public class WebSocketConfig implements WebSocketConfigurer {

    @Bean
    public WebSocketHandler webSocketHandler() {
        // 创建并返回 WebSocket 处理类的实例
        return new WebSocketHandler();
    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        // 注册 WebSocket 处理器及其访问路径
        registry.addHandler(webSocketHandler(), "/ws")
                .setAllowedOrigins("*");  // 设置允许的来源，解决跨域问题
    }
}
